home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0019_RANDOM NUMBER GENERATOR.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-31  |  3KB  |  103 lines

  1. ==============================================================================
  2.  BBS: The Sand Box BBS - SourceNet Central HUB
  3.   To: JUD MCCRANIE                 Date: 12-17─92 (16:42)
  4. From: TREVOR CARLSEN             Number: 531    [87] FD-Pascal
  5. Subj: BP 7 DIFFERENCE            Status: Public
  6. ------------------------------------------------------------------------------
  7.  JM> The behavior of RANDOM (with RandSeed set) is different in
  8.  JM> BP7 (and presumably TP7) from that in TP 5.5.  (I don't know
  9.  JM> how TP 6 compares since I burned it off my disk).
  10.  
  11.  JM> RandSeed := 123;
  12.  JM> for i := 1 to 8 do writeln( random( 1000));
  13.  
  14.  JM> TP 5.5: 343 282 986 996 781 855 343  32
  15.  JM> BP 7.0: 859  80 869 854 317 257  20  46
  16.  
  17.  JM> ...both are consistant, but they are different sequences.
  18.  JM> This can have some dire consequences.  ...
  19.  
  20. It certainly could if you did not know about it and unfortunately I can
  21. find no reference to the changes in the documentation. (Richard Nelson?)
  22.  
  23. Here is a fix (supplied to me via Netmail courtesy Joe Lamoine - thanks Joe).
  24.  
  25. >Quote........
  26.  
  27. I posted a message on Compuserve last nite and got the following
  28. unit in a response.  It seems to work fine!
  29.  
  30.  
  31. { *  Turbo Pascal Runtime Library Version 6.0     * ;
  32.   *  Random Number Generator                      * ;
  33.   *                                               * ;
  34.   *  Copyright (C) 1988,92 Borland International  * }
  35.  
  36.  unit TP6Rand;
  37.  
  38.  interface
  39.  
  40.  function Random(Max: Integer): Integer;
  41.  
  42.  implementation
  43.  
  44.  const
  45.   { Scaling constant}
  46.   ConstM31 = Longint(-31);
  47.   { Multiplication factor}
  48.   Factor: Word = $8405;
  49.  
  50.  
  51.  function NextRand: Longint; assembler;
  52.  { Compute next random number
  53.   New := 8088405H * Old + 1
  54.   Out  DX:AX = Next random number
  55.  }
  56.  asm
  57.   MOV  AX,RandSeed.Word[0]
  58.   MOV  BX,RandSeed.Word[2]
  59.   MOV  CX,AX
  60.   MUL  Factor.Word[0]     { New = Old.w0 * 8405H }
  61.   SHL  CX,1               { New.w2 += Old.w0 * 808H }
  62.   SHL  CX,1
  63.   SHL  CX,1
  64.   ADD  CH,CL
  65.   ADD  DX,CX
  66.   ADD  DX,BX              { New.w2 += Old.w2 * 8405H }
  67.   SHL  BX,1
  68.   SHL  BX,1
  69.   ADD  DX,BX
  70.   ADD  DH,BL
  71.   MOV  CL,5
  72.   SHL  BX,CL
  73.   ADD  DH,BL
  74.   ADD  AX,1      { New += 1 }
  75.   ADC  DX,0
  76.   MOV  RandSeed.Word[0],AX
  77.   MOV  RandSeed.Word[2],DX
  78.  end;
  79.  
  80. function Random(Max: Integer): Integer; assembler;
  81.  asm
  82.   CALL  NextRand
  83.   XOR   AX,AX
  84.   MOV   BX,Max.Word[0]
  85.   OR    BX,BX
  86.   JE    @@1
  87.   XCHG  AX,DX
  88.   DIV   BX
  89.   XCHG  AX,DX
  90.  @@1:
  91.  end;
  92.  
  93. end.
  94.  
  95. >End of quote.
  96.  
  97.  
  98. TeeCee
  99.  
  100.  
  101. --- TC-ED   v2.01
  102.  * Origin: The Pilbara's Pascal Centre (+61 91 732930) (3:690/644)
  103.